﻿<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Médiathèque - Emprunt</title>
        <?php include('communs/lienCssAdmin.php'); ?>
        <script type="text/javascript" src="js/dropdown/jquery.js"></script>
    </head>
    <body>
        <div id="conteneurTete">
            <?php include('communs/enTete.php'); ?>
        </div> 
        <?php include('communs/menuAdmin.php'); ?>   
        <div id="conteneurCorps">
            <hr class="resultat">
            <?php
                echo "<form name='formEmprunt' method='GET'action='emprunt.php'>";
                    echo "<label for='matriculeEmprunt'>Matricule:</label>";
                    echo "<input type='text' name='matriculeEmprunt' id='matriculeEmprunt'value='";
                        if (isset($_GET['matriculeEmprunt']))
                        {
                            echo $_GET['matriculeEmprunt'];
                        }
                    echo "'/>";
                    echo "</br>";
                    echo "<label for='codeMedia'>Code du média:</label>";
                    echo "<input type='text' name='codeMedia' id='codeMedia' value='";
                        if (isset($_GET['codeMedia']))
                        {
                            echo $_GET['codeMedia'];
                        }
                    echo "'/>";
                    echo "</br>";
                    echo "<input type='submit'/>";
                echo "</form>";
                
                if (($_GET['matriculeEmprunt']<>"") and ($_GET['codeMedia']<>""))
                    {
                    ajoutEmprunt($_GET['matriculeEmprunt'], $_GET['codeMedia']);
                    }
                elseif($_GET['codeMedia']<>"")
                    {
                    afficherTableau($_GET['codeMedia'], 2);
                    }
                elseif ($_GET['matriculeEmprunt']<>"")
                    {
                    afficherTableau($_GET['matriculeEmprunt'], 1);
                    }
                else
                    {
                    echo "Entrez des éléments";
                    }
            ?>
            <hr class="resultat">
            <a href="indexAdmin.php">Retour</a>
        </div>
        <div id="conteneurPied">
            <?php include('communs/piedDePage.php'); ?>   
        </div>
    </body>
</html>


<?php 
function afficherTableau($noElement, $typeElement, $noElementSecond = null)
{
    require('php/connexionBaseDeDonnees.php');
    /*echo $typeElement."  ".$noElement." ".$noElementSecond;*/
    if ($typeElement = 1)
        {
        $reponse = $bd->query("select concat(client.nom,', ',client.prenom) as nomClient, emprunt.dateEmprunt as dateEmprunt, emprunt.dateDateRetourPrevue as dateRetourPrevue, emprunt.dateRetourReel as dateRetourReelle, emprunt.idExemplaire as idExemplaire, exemplaire.idmedia as idMedia
                               from client, emprunt, exemplaire 
                               where client.matricule = emprunt.matricule and exemplaire.id = emprunt.idExemplaire and client.matricule = ".$noElement." 
                               order by dateEmprunt desc");
        }
    elseif ($typeElement = 2)
        {
        $reponse = $bd->query("select concat(client.nom,', ',client.prenom) as nomClient, emprunt.dateEmprunt as dateEmprunt, emprunt.dateDateRetourPrevue as dateRetourPrevue, emprunt.dateRetourReel as dateRetourReelle, emprunt.idExemplaire as idExemplaire, exemplaire.idmedia as idMedia
                               from client, emprunt, exemplaire 
                               where client.matricule = emprunt.matricule and exemplaire.id = emprunt.idExemplaire and exemplaire.id = ".$noElement);
        }
    /*
    elseif ($typeElement = 3)
        {
        $reponse = $bd->query("select concat(client.nom,', ',client.prenom) as nomClient, emprunt.dateEmprunt as dateEmprunt, emprunt.dateDateRetourPrevue as dateRetourPrevue, emprunt.dateRetourReel as dateRetourReelle, emprunt.idExemplaire as idExemplaire, exemplaire.idmedia as idMedia
                               from client, emprunt, exemplaire 
                               where client.matricule = emprunt.matricule and exemplaire.id = emprunt.idExemplaire and client.matricule = ".$noElement." and exemplaire.id = ".$noElementSecond);
       ajoutEmprunt($noElement, $noElementSecond);
        }
    */
    $listeDeEmprunt = $reponse->fetchAll();
    /*print_r($listeDeEmprunt);*/
    echo "<table border=1'>";
            echo '<caption>Liste Emprunt</caption>';/*Ajouter nom*/
            echo '<tr>';
                echo '<th>Nom item</th>';
                echo "<th>Date d'Emprunt</th>";
                echo "<th>Retour prévu</th>";
                echo "<th>Retour réel</th>";
                echo "<th>         </th>";
            echo '</tr>';
    foreach($listeDeEmprunt as $emprunt)
    {
        $reponseMedia = $bd->query("select 'disque' as type from disque where id=$emprunt[idMedia]
                                    union select 'document' as type from document where id=$emprunt[idMedia]
                                    union select 'film' as type from film where id=$emprunt[idMedia]
                                    union select 'piste' as type from piste where id=$emprunt[idMedia]
                                    union select 'episode' as type from episode where id=$emprunt[idMedia]");
        $media = $reponseMedia->fetch();
        $reponseNom = $bd->query("select titre as titreMedia
                                  from $media[type]
                                  where id = $emprunt[idMedia]");
        $nomMedia = $reponseNom->fetch();
        echo '<tr>';
        echo "<td>$nomMedia[titreMedia]</td>";
        echo "<td>$emprunt[dateEmprunt]</td>";
        echo "<td>$emprunt[dateRetourPrevue]</td>";
        if ($emprunt['dateRetourReelle'] <> null )
            {
            echo "<td>$emprunt[dateRetourReelle]</td>";
            echo "<td> </td>";
            }
        else
            {
            echo "<td> </td>";
            echo "<td><a href=''>Retour</a></td>";
            }
        echo '</tr>';
    }
    echo '</table>';
}


function ajoutEmprunt($noMatricule, $noExemplaire)
{
    require('php/connexionBaseDeDonnees.php');
    $date = date("Y-m-d");
    $date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " +2 week"));
    /*echo $date;*/
    $requete = $bd->prepare(
        "insert into emprunt(matricule, idExemplaire, dateEmprunt, dateDateRetourPrevue) ".
        "values(:matricule, :idExemplaire, CURRENT_DATE(), :dateDateRetourPrevue)");
    $reussi = $requete->execute(array(
        ':matricule'            => $noMatricule,
        ':idExemplaire'         => $noExemplaire,
        ':dateDateRetourPrevue' => $date));
        
    if($reussi = false)
    {
        echo 'L\'ajout de l\'emprunt dans la bd a échoué.';
    }
    else
    {
    afficherTableau($noMatricule, 1);
    }
}

?>


<!--
0860624
$date = strtotime(date("Y-m-d", strtotime(CURRENT_DATE())) . " +2 week");

select concat(client.nom,", ",client.prenom) as nomClient, emprunt.dateEmprunt as dateEmprunt, emprunt.dateDateRetourPrevue as dateRetourPrevue, emprunt.dateRetourReel as dateRetourReelle, emprunt.idExemplaire as idExemplaire, exemplaire.idmedia as idMedia
from client, emprunt, exemplaire 
where client.matricule = emprunt.matricule and exemplaire.idExemplaire = emprunt.idExemplaire

select 'disque' from disque where id=$emprunt[idMedia]
union select 'document' from document where id=$emprunt[idMedia]
union select 'film' from film where id=$emprunt[idMedia]
union select 'piste' from piste where id=$emprunt[idMedia]
union select 'episode' from episode where id=$emprunt[idMedia]

select titre
from $media[type]
where id = $emprunt[idMedia]

-->

